package com.ly.lypopupwindowdemo.activity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Android Dialog使用详(DialogFragment)
 * http://www.jianshu.com/p/5b34c1a72d03
 * MD风格的AlertDialog
 * http://www.jianshu.com/p/6caffdbcd5db
 */
public class DialogActivity extends AppCompatActivity {

    @BindView(R.id.zhishidian)
    TextView zhishidian;
    @BindView(R.id.content)
    TextView content;

    boolean isclose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        ButterKnife.bind(this);

        zhishidian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isclose) {
                    content.setVisibility(View.VISIBLE);
                    isclose = true;
                } else {
                    content.setVisibility(View.GONE);
                    isclose = false;
                }
            }
        });

        content.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    /**
     * 这个 V7 包中的 AlertDialog 在 Android 2.1 以上可以提供兼容性的 Material 风格 Dialog 。
     * 也就是说，使用这个包中的 AlertDialog 的话，从2.1到6.0都是 Material风格的 Dialog 。
     *
     * @param view
     */
    public void showMDAlertDialog(View view) {
        /**
         * 得到了builde之后就可以进行各种属性的设置
         */

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("标题");
        builder.setMessage("这颗树直么？");

        final EditText edittext = new EditText(this);
        builder.setView(edittext, 50, 50, 50, 50);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String str = edittext.getText().toString();
                Toast.makeText(DialogActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNeutralButton("中性", null);//中性按钮
        /**
         * 设置一个自定义的View放置到message的下方，可以是一个View对象，也可以是一个view资源ID。 如:
         * 对于使用View对象的参数，还可以设置他的边距
         * builder.setView(new EditText(this),20,20,20,20);
         */

        builder.show();
    }

    /**
     * setMultiChoiceItems
     * <p>
     * 第一个参数为列表项的文本数组，第二个参数为列表项默认情况下的选中状态，为空则都不选中，第三个是列表项的监听器类。在使用这个方法的时候如果同时使用了setMessage方法会导致此方法无效，而只显示setMessage的内容。 如:
     * builder.setMultiChoiceItems(new String[]{"233333", "hahahaha"}, null, null);
     *
     * @param view
     */

    public void showMultiAlertDialog(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("这是一个多选的AlertDialog");
        builder.setMultiChoiceItems(new String[]{"稻香", "半岛铁盒", "一路向北"}, new boolean[]{true, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(DialogActivity.this, "which===" + which + "isChecked==" + isChecked, Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNeutralButton("中性", null);//中性按钮
        builder.show();
    }

    /**
     * setItems
     * <p>
     * 第一个参数为列表项的文本数组，第二个参数为列表项的监听器类。在使用这个方法的时候如果同时使用了setMessage方法会导致此方法无效，而只显示setMessage的内容。 如:
     * builder.setItems(new String[]{"Item1", "Item2"}, null);
     *
     * @param view
     */

    public void showSingleAlertDialog(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("这是一个单选的AlertDialog");
        builder.setItems(new String[]{"稻香", "半岛铁盒", "一路向北"}, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "点击which==" + which, Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    public void showAlertDialogWithIcon(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("带图标的AlertDialog");
        builder.setIcon(R.mipmap.list_icon_hongbao);
        builder.setPositiveButton("确定", null);
        builder.show();
    }

    /**
     * 自定义Dialog
     *
     * @param view
     */
    public void showModifyDialog(View view) {

        final AlertDialog builder = new AlertDialog.Builder(this).create();
        builder.setTitle("标题");
        builder.setMessage("这颗树直么？");

        builder.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "点击否", Toast.LENGTH_SHORT).show();
                builder.dismiss();
            }
        });

        builder.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "点击是", Toast.LENGTH_SHORT).show();
                builder.dismiss();
            }
        });
        builder.show();

        Button posiviveButton = builder.getButton(DialogInterface.BUTTON_POSITIVE);
        posiviveButton.setTextColor(Color.RED);
        Button negativeButton = builder.getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(Color.parseColor("#333333"));
    }

}
